$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'supplier/searchSupplierInfo.do',
        datatype: "json",
        colModel: [
            { label: '供货商编号', name: 'supplierNo', index: "supplierNo", width: 100, key: true },
            { label: '供货商名称', name: 'supplierName', index: "supplierName", width: 90 },
            { label: '地址', name: 'address', width: 100 },
            { label: '手机号', name: 'phone', width: 80 },
            { label: '供货范围', name: 'supplierRange', width: 80 },
            { label: '备注', name: 'remark', width: 100 }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
        	 root: "page.list",
             page: "page.currPage",
             total: "page.totalPage",
             records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
    	supplierName:null,
        showList: true,
        showSupplierNo:false,
        title:null,
        supplier:{
            supplierNo:null,
        	supplierName:null,
        	address:null,
        	phone:null,
        	supplierRange:null,
        	remark:null
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.title = "新增";
            vm.supplier = {
             supplierNo:null,
             supplierName:null,
       		 address:null,
       		 phone:null,
       	     supplierRange:null,
       		 remark:null
             };
            vm.showList = false;
        },
        update: function () {
            var supplierNo = getSelectedRow();
            if(supplierNo == null){
                return ;
            }
            vm.title = "修改";
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "supplier/getSupplierByNo.do?supplierNo="+supplierNo,
                contentType: "application/json",
                success: function(r){
                    if(r.code === 0){
                    	vm.supplier=r.result;
                    }
                }
            });
            vm.showSupplierNo=true;
            vm.showList = false;
        },
        del: function () {
            var supplierNos = getSelectedRows();
            if(supplierNos == null){
                return ;
            }
            var flag=confirm('确定要删除选中的记录？');
            if(flag){
                $.ajax({
                    type: "POST",
                    url: baseURL + "/supplier/deleteByNo.do",
                    contentType: "application/json",
                    data: JSON.stringify(supplierNos),
                    success: function(r){
                        if(r.code == 0){
                        	alert("删除成功");
                        	 vm.reload();
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            }
        },
        saveOrUpdate: function () {
        	var supplierName=vm.supplier.supplierName;
        	var supplierRange=vm.supplier.supplierRange;
        	var msg="";
        	if(supplierName==""||supplierName==null){
        		msg=msg+"名称不能为空,"
        	}
        	if(supplierRange==""||supplierRange==null){
        		msg=msg+"供货范围不能为空,"
        	}
        	if(msg!=""){
        		 alert(msg);
        	}else{
        		var url = vm.supplier.supplierNo == null ? "supplier/add.do" : "supplier/updateSupplierInfo.do";
        		$.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.supplier),
                    success: function(r){
                        if(r.code === 0){
                        	alert("操作成功");
                        	vm.reload();
                        }else{
                            alert(r.msg);
                        }
                    }
                });
        	}
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'supplierName': vm.supplierName},
                page:page
            }).trigger("reloadGrid");
        }
    }
});